using Microsoft.AspNetCore.Mvc;
using MyHomeworkWebAPi.Models.DTOs;
using MyHomeworkWebAPi.Models.ef;
using MyHomeworkWebAPi.Models.Request;
using MyHomeworkWebAPi.Models.response;
using MyHomeworkWebAPi.Services;

namespace MyHomeworkWebAPi.Controllers
{
    /// <summary>
    /// 商品管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ProductsController : ControllerBase
    {
        private readonly IProductServices _productServices;

        /// <summary>
        /// 构造函数，注入商品服务
        /// </summary>
        /// <param name="productServices">商品服务接口</param>
        public ProductsController(IProductServices productServices)
        {
            _productServices = productServices;
        }

        /// <summary>
        /// 获取所有商品
        /// </summary>
        /// <returns>商品列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            var products = await _productServices.GetAllAsync();
            return Ok(ApiResponse<List<ProductDTO>>.SuccessResponse(products));
        }

        /// <summary>
        /// 根据ID获取商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <returns>商品详情</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetById(Guid id)
        {
            var product = await _productServices.GetByIdAsync(id);
            if (product == null)
            {
                return NotFound(ApiResponse<ProductDTO>.ErrorResponse("商品不存在", 404));
            }
            return Ok(ApiResponse<ProductDTO>.SuccessResponse(product));
        }

        /// <summary>
        /// 创建商品
        /// </summary>
        /// <param name="userId">发布者ID</param>
        /// <param name="request">商品信息</param>
        /// <returns>创建的商品信息</returns>
        [HttpPost("user/{userId}")]
        public async Task<IActionResult> Create(Guid userId, [FromBody] ProductCreateRequest request)
        {
            try
            {
                var product = await _productServices.CreateAsync(userId, request);
                return Ok(ApiResponse<ProductDTO>.SuccessResponse(product, "商品创建成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<ProductDTO>.ErrorResponse(ex.Message, 400));
            }
        }

        /// <summary>
        /// 更新商品状态
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="status">新状态</param>
        /// <returns>更新后的商品信息</returns>
        [HttpPut("{id}/status")]
        public async Task<IActionResult> UpdateStatus(Guid id, [FromQuery] string status)
        {
            try
            {
                var product = await _productServices.UpdateStatusAsync(id, status);
                return Ok(ApiResponse<ProductDTO>.SuccessResponse(product, "状态更新成功"));
            }
            catch (Exception ex)
            {
                return NotFound(ApiResponse<ProductDTO>.ErrorResponse(ex.Message, 404));
            }
        }

        /// <summary>
        /// 获取用户发布的商品
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>该用户发布的商品列表</returns>
        [HttpGet("user/{userId}")]
        public async Task<IActionResult> GetByUserId(Guid userId)
        {
            var products = await _productServices.GetByUserIdAsync(userId);
            return Ok(ApiResponse<List<ProductDTO>>.SuccessResponse(products));
        }

        /// <summary>
        /// 根据分类获取商品
        /// </summary>
        /// <param name="categoryId">分类ID</param>
        /// <returns>该分类下的商品列表</returns>
        [HttpGet("category/{categoryId}")]
        public async Task<IActionResult> GetByCategory(Guid categoryId)
        {
            var products = await _productServices.GetByCategoryAsync(categoryId);
            return Ok(ApiResponse<List<ProductDTO>>.SuccessResponse(products));
        }

        /// <summary>
        /// 搜索商品
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <returns>匹配的商品列表</returns>
        [HttpGet("search")]
        public async Task<IActionResult> Search([FromQuery] string keyword)
        {
            var products = await _productServices.SearchAsync(keyword);
            return Ok(ApiResponse<List<ProductDTO>>.SuccessResponse(products));
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var result = await _productServices.DeleteAsync(id);
            if (!result)
            {
                return NotFound(ApiResponse<bool>.ErrorResponse("商品不存在", 404));
            }
            return Ok(ApiResponse<bool>.SuccessResponse(true, "商品删除成功"));
        }

        /// <summary>
        /// 更新商品信息
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="request">更新的商品信息</param>
        /// <returns>更新后的商品信息</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, [FromBody] ProductCreateRequest request)
        {
            try
            {
                var product = await _productServices.UpdateAsync(id, request);
                return Ok(ApiResponse<ProductDTO>.SuccessResponse(product, "商品更新成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<ProductDTO>.ErrorResponse(ex.Message, 400));
            }
        }
    }
} 